<?php
/**
 * The database class.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since	2013-10-12
 */
require_once 'login_info.php';

class database {
	var $mysqli;

	/**
	 * The constructor of the database class.
	 *
	 * @param user	  The username of the MySQL database.
	 * @param passwd  The password of the user of the MySQL database.
	 * @param host	  The hostname of the MySQL database.
	 * @return
	 */
	function __construct($user = NULL, $passwd = NULL, $host = "localhost") {
		if (!$user) {
			$user = $_COOKIE[loginInfo::user_cookie];
		}
		if (!$passwd) {
			$passwd = $_COOKIE[loginInfo::passwd_cookie];
		}
		$this->mysqli = new mysqli($host, $user, $passwd, "opol");
		if ($this->mysqli->connect_errno) {
			// Print an error message, as the user was unable to login.
            //echo "Invalid username/password.<br>\n";
		}
	}

    /**
     * Sanitizes a string for input.
     *
     * @param string	The string to sanitize.
     * @return			The sanitized string.
     */
    function sanitize_data ($string) {
        $ret = $this->mysqli->escape_string ($string);
        return $ret;
    }

	/**
	 * Run a query on the database.
	 *
	 * @param q	 The query string to run on the database.
	 * @return	 The result of the query.
	 */
	function query($q) {
		$result = $this->mysqli->query($q);
		return $result;
	}

	/**
	 * Calls the prepare function on the mysqli object.
	 */
	function prepare($s) {
		$result = $this->mysqli->prepare($s);
		return $result;
	}

	/**
	 * Returns the connect_errno member of the myslqi object.
	 */
	function connect_errno () {
		return $this->mysqli->connect_errno;
	}

	/**
	 * Returns the error member of the mysqli object.
	 */
	function error() {
		return $this->mysqli->error;
	}

	/**
	 * Returns the errno member of the mysqli object.
	 */
	function errno() {
		return $this->mysqli->errno;
	}

	/**
	 * Returns the insert_id member of the mysqli object.
	 */
	function insert_id() {
		return $this->mysqli->insert_id;
	}

	// $columns is an associative array
	//	 ColumnName ==> ColumnType
	/**
	 * Create a table in the database.
	 *
	 * @param name	   The name of the table to create.
	 * @param columns  An associative array of column names to column types.
	 * @return		   Something.
	 */
	function create_table($name,$columns) {
		$query = "CREATE TABLE " . $name . " (";
		foreach ($columns as $column_name => $column_type) {
			$query .= $column_name . " " . $column_type . ",";
		}
		$query = trim($query, ",") . ")";
		$query .= ";";

		$result = $this->query($query);

		return 0;
	}

	/**
	 * Inserts an entry into a table in the database.
	 *
	 * @param table	  The name of the table in which to insert an entry.
	 * @param values  An associative array of column names to the values.
	 * @return		  Something.
	 */
	function insert_entry($table,$values) {
		$query = "INSERT INTO " . $table . " ";
		$cols = "(";
		$vals = "(";
		foreach ($values as $column_name => $value) {
			$cols .= $column_name . ",";
            $value = $this->sanitize_data ($value);
			if (is_string ($value)) {
				$vals .= "'" . $value . "',";
			} else {
				$vals .= $value . ",";
			}
		}
		$cols = trim($cols, ",") . ")";
		$vals = trim($vals, ",") . ")";
		$query .= $cols . " VALUES " . $vals . ";";

		$result = $this->query($query);

		return $result;
	}

	/**
	 * Selects entries from a table in the database.
	 *
	 * @param table	   The name of the table from which to select entries.
	 * @param columns  An array of column names, or NULL to select all.
	 * @param where	   The where conditions as a string, or NULL for none.
	 * @return		   The result of the selection.
	 */
	function select_entry($table,$columns = NULL,$where = NULL) {
		$query = "SELECT ";
		if ($columns) {
			foreach ($columns as $column_name) {
				$query .= $column_name . ",";
			}
			$query = trim($query, ",");
		} else {
			$query .= "*";
		}

		$query .= " FROM " . $table;

		if ($where) {
			$query .= " WHERE " . $where;
		}

		$query .= ";";
		$result = $this->query($query);

		return $result;
	}

	/**
	 * Selects a row in the database by the row's ID.
	 *
	 * @param table	 The table name to select.
	 * @param id	 The id of the row to select.
	 * @return		 An associative array of the values in the row.
	 */
	function select_row_by_id ($table, $id) {
		$where = sprintf ("id=%d", $id);
		$result = $this->select_entry ($table, NULL, $where);
		$assoc = $result->fetch_assoc ();
		return $assoc;
	}

	/**
	 * Selects a single column in the database by the row's ID.
	 *
	 * @param table	  The name of the table from which to select.
	 * @param column  The name of the column from which to select.
	 * @param id	  The id of the row from which to select.
	 * @return		  The value of the column in the row.
	 */
	function select_col_by_id ($table, $column, $id) {
		$assoc = $this->select_row_by_id ($table, $id);
		$ret = $assoc[$column];
		return $ret;
	}

	/**
	 * Update an entry in the table.
	 *
	 * @param table	   The name of the table in which to update entries.
	 * @param columns  An associative array of column names mapped to values.
	 * @param where	   The where conditions for the update, or NULL for none.
	 * @return		   Something.
	 */
	function update_entry($table, $columns, $where = NULL) {
		$query = "UPDATE " . $table . " SET ";
		foreach ($columns as $column_name => $column_value) {
            //$column_value = $this->sanitize_data ($column_value);
			$query .= $column_name . "=" . $column_value . ",";
		}
		$query = trim($query, ",");

		if ($where) {
			$query .= " WHERE " . $where;
		}
		$query .= ";";

		$result = $this->query($query);

		return $result;
	}

	/**
	 * Updates an entry by id.
	 *
	 * @param table		The name of the table from which to update entries.
	 * @param columns	The columns in the table to update.
	 * @param id		The id of the object to update.
	 * @return			The result of the update.
	 */
	function update_entry_by_id ($table, $columns, $id) {
		$where = sprintf ("id=%d", $id);
		$result = $this->update_entry ($table, $columns, $where);
		return $result;
	}

	/**
	 * Close the connection to the database.
	 */
	function close () {
		$result = $this->mysqli->close ();
		return $result;
	}
}
?>
